/***************************************************************************
 *   Copyright © 2010 by Nikolay Kornaev                                   *
 *   n.kornaev@gmail.com                                                   *
 *                                                                         *
 *                    This file is part of neuma.                          *
 *                                                                         *
 *   neuma is free software; you can redistribute it and/or modify it      *
 *   under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   neuma is distributed in the hope that it will be useful,              *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include <QtGui>
#include "SettingsDialog.h"

namespace Neuma
{
  SettingsDialog::SettingsDialog(SettingsHandler *sh, QWidget *parent) :
    QDialog(parent)
  {
    setupUi(this);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    connect(bColorNeuma, SIGNAL(clicked()), SLOT(processColor()));
    connect(bColorMarks, SIGNAL(clicked()), SLOT(processColor()));
    connect(bColorSyllabe, SIGNAL(clicked()), SLOT(processColor()));
    connect(bColorOutlinePage, SIGNAL(clicked()), SLOT(processColor()));
    connect(bColorOutlineNeuma, SIGNAL(clicked()), SLOT(processColor()));
    connect(bColorOutlineSyllabe, SIGNAL(clicked()), SLOT(processColor()));

    sbHorizontalOffset->setValue(sh->val(RendererSpaceBlocksHorizontal).toDouble());
    sbVerticalOffset->setValue(sh->val(RendererSpaceBlocksVertical).toDouble());
    sbLinesSpace->setValue(sh->val(RendererSpaceLines).toDouble());

    QStringList fontsN = sh->val(RendererFontsListNeuma).toStringList();
    fontsN.removeDuplicates();
    cbFontNeuma->addItems(fontsN);
    int cur = cbFontNeuma->findText(sh->val(RendererFontNameNeuma).toString());
    cbFontNeuma->setCurrentIndex(cur);

    QStringList fontsS = sh->val(RendererFontsListSyllabe).toStringList();
    fontsS.removeDuplicates();
    cbFontSyllabe->addItems(fontsS);
    cur = cbFontSyllabe->findText(sh->val(RendererFontNameSyllabe).toString());
    cbFontSyllabe->setCurrentIndex(cur);

    sbFontSizeNeuma->setValue(sh->val(RendererFontSizeNeuma).toDouble());
    sbFontSizeSyllabe->setValue(sh->val(RendererFontSizeSyllabe).toDouble());

    QColor nColor = QColor(sh->val(RendererFontColorNeuma).toString());
    applyColor(leColorPreviewNeuma, nColor);

    QColor mColor = QColor(sh->val(RendererFontColorMarks).toString());
    applyColor(leColorPreviewMarks, mColor);

    QColor sColor = QColor(sh->val(RendererFontColorSyllabe).toString());
    applyColor(leColorPreviewSyllabe, sColor);

    bool oP = sh->val(RendererOutlinePage).toBool();
    bool oN = sh->val(RendererOutlineNeuma).toBool();
    bool oS = sh->val(RendererOutlineSyllabe).toBool();
    chOutlinePage->setChecked(oP);
    chOutlineNeuma->setChecked(oN);
    chOutlineSyllabe->setChecked(oS);
    bColorOutlinePage->setEnabled(oP);
    bColorOutlineNeuma->setEnabled(oN);
    bColorOutlineSyllabe->setEnabled(oS);

    QColor pOColor = QColor(sh->val(RendererOutlineColorPage).toString());
    applyColor(leColorPreviewOutlinePage, pOColor);

    QColor nOColor = QColor(sh->val(RendererOutlineColorNeuma).toString());
    applyColor(leColorPreviewOutlineNeuma, nOColor);

    QColor sOColor = QColor(sh->val(RendererOutlineColorSyllabe).toString());
    applyColor(leColorPreviewOutlineSyllabe, sOColor);

    leExportResolution->setText(QString("%1").arg(sh->val(RendererExportResolution).toInt()));
    cbNeumaOverSyllabe->setChecked(sh->val(RendererNeumaOverSyllabe).toBool());
    cbNeumaTruncateMarks->setChecked(sh->val(RendererNeumaTruncateMarks).toBool());
  }

  void SettingsDialog::processColor()
  {
    QColor color;
    QLineEdit *le;

    QPushButton *pb = qobject_cast<QPushButton *> (sender());
    if (pb == bColorNeuma)
      le = leColorPreviewNeuma;
    else if (pb == bColorMarks)
      le = leColorPreviewMarks;
    else if (pb == bColorSyllabe)
      le = leColorPreviewSyllabe;
    else if (pb == bColorOutlinePage)
      le = leColorPreviewOutlinePage;
    else if (pb == bColorOutlineNeuma)
      le = leColorPreviewOutlineNeuma;
    else if (pb == bColorOutlineSyllabe)
      le = leColorPreviewOutlineSyllabe;
    else return;

    color = le->palette().color(QPalette::Background);
    color = QColorDialog::getColor(color, this);
    if (color.isValid())
      applyColor(le, color);
  }

  void SettingsDialog::applyColor(QLineEdit *le, QColor color)
  {
    le->setText(color.name());
    le->setPalette(QPalette(color));
    le->setAutoFillBackground(true);
  }

  QStringList SettingsDialog::collectComboBox(QComboBox *cb, bool clear)
  {
    QStringList out;
    out << cb->lineEdit()->text();
    while (cb->count())
    {
      int i = cb->currentIndex();
      out << cb->itemText(i);
      cb->removeItem(i);
    }
    out.removeDuplicates();
    if (!clear)
      cb->addItems(out);
    return out;
  }

}
